#include "temperature_sensor.h"
#include "driver/temperature_sensor.h"
#include "esp_log.h"

static const char *TAG = "TEMP_SENSOR";
static temperature_sensor_handle_t temp_sensor = NULL;

void temperature_sensor_init(void) {
    temperature_sensor_config_t temp_sensor_config = {
        .range_min = -10,
        .range_max = 80,
    };
    ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
    ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
    ESP_LOGI(TAG, "Temperature sensor initialized");
}

float read_temperature(void) {
    float tsens_out;
    ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tsens_out));
    ESP_LOGI(TAG, "Temperature: %.2f ℃", tsens_out);
    return tsens_out;
}